package com.summer.JUnit;

import java.io.BufferedReader;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.StringTokenizer;

import junit.framework.TestCase;

public class TestLargestDataFile extends TestCase {

	public TestLargestDataFile(String mothod){
		super(mothod);
	}
	
	/**
	 * Run all the tests in testdata.txt (does not test exception case). 
	 * we'll get an error if any of the file I/O goes wrong.
	 * */
	public void testFromFile() throws Exception{
		String line;
		@SuppressWarnings("resource")
		BufferedReader rdr = new BufferedReader(new FileReader("bin/com/summer/JUnit/testdata.txt"));
		
		while((line = rdr.readLine()) != null){
			//Ignore comments
			if(line.startsWith("#"))
				continue;
			
			StringTokenizer st = new StringTokenizer(line);
			//Blank line
			if(!st.hasMoreTokens())
				continue;
			
			//Get the expected value
			String val = st.nextToken();
			int expected = Integer.valueOf(val).intValue();
			
			//And the arguments to Largest
			ArrayList<Object> argument_list = new ArrayList<>();
			while(st.hasMoreTokens()){
				argument_list.add(Integer.valueOf(st.nextToken()));
			}
			
			//Transfer object list into native array
			int[] arguments = new int[argument_list.size()];
			for(int i=0;i < argument_list.size();i++){
				arguments[i] = ((Integer)argument_list.get(i)).intValue();
			}
			
			//And run the assert
			assertEquals(expected, Largest.largest(arguments));
			
		}
		
	}
	
}
